#! /usr/bin/python
# -*- coding: cp1250 -*-

import glob, re, sys, string, os
import pepe.p_txt.bigtxt as bt
import pepe.p_filesystem.basic as pfb

def main(OPTIONS='', PATTERN='', FILE=''):
    '''
    Purpose: Search files
    Syntax: fnd OPTIONS PATTERN FILE
    Example: fnd -rv "^data2007" *.txt

    OPTIONS:
    -r  recursive
    -v  verbose
    '''
    if 'v' in OPTIONS:
        msg = """:: fnd.py OPTIONS="%s" PATTERN="%s" FILE="%s"\n::"""
        print msg % (OPTIONS, PATTERN, FILE)

    if 'r' in OPTIONS:
        files = pfb.fslist(root='.', patterns=FILE,
                           recurse=True, return_folders=False)
    else:
        files = glob.glob(FILE)

    for line in sorted(files):
        m = re.match(PATTERN, line)
        if m:
            print line
    return 1

if __name__ == '__main__':

    if len(sys.argv) == 4:
        os.system('@chcp 1250')
        main(sys.argv[1], sys.argv[2], sys.argv[3])
    elif len(sys.argv) == 3:
        os.system('@chcp 1250')
        main(OPTIONS='', PATTERN=sys.argv[1], FILE=sys.argv[2])
    else:
        print main.__doc__

    #print main.__doc__
    #main("-", "^import", "*.py")
